package org.zfes.snowier.zbss.xzy.biz.config.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.core.data.ParamMap;
import org.zfes.snowier.zbss.xzy.biz.config.dao.ConfigCateMapper;
import org.zfes.snowier.zbss.xzy.biz.config.model.ConfigCate;
import org.zfes.snowier.zbss.xzy.biz.config.service.IConfigCateService;

import java.util.stream.Stream;
@Service
public class ConfigCateServiceImpl implements IConfigCateService{
 	@Autowired
	private ConfigCateMapper aIConfigCateMapper;
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createAIConfigCate(ConfigCate aIConfigCate) {
		ZBeanUtil.setCreateInfo(aIConfigCate);
		ZBeanUtil.validateBean(aIConfigCate);
		
		Object id7=aIConfigCateMapper.selectIdByName(aIConfigCate.getName());
		if(id7!=null){
			ZAlert.Error("名称已经存在");
		}
		aIConfigCateMapper.insertSelective(aIConfigCate);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<ConfigCate> loadAIConfigCateById(Long id) {
		if(id!=null){
			return Optional.ofNullable(aIConfigCateMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<ConfigCate> loadCateByAlgorithmId(Long algorithmId){
		if(algorithmId!=null){
			return Optional.ofNullable(aIConfigCateMapper.selectByAlgorithmId(algorithmId));
		}
		return Optional.empty();
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateAIConfigCate(ConfigCate aIConfigCate) {
		ZBeanUtil.setUpdatedInfo(aIConfigCate);
		ZBeanUtil.validateBean(aIConfigCate);
		Object id7=aIConfigCateMapper.selectIdByName(aIConfigCate.getName());
		if(id7!=null&&!id7.equals(aIConfigCate.getId())){
			ZAlert.Error("名称已经存在");
		}
		aIConfigCateMapper.updateByPrimaryKeySelective(aIConfigCate);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteAIConfigCate(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		aIConfigCateMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setAIConfigCateStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		aIConfigCateMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadAIConfigCateDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		pm.getObj("name").ifPresent(v->pm.updateParam("name", "%"+v+"%"));
		DataSet ds=DataSet.newDS2(aIConfigCateMapper.selectListCount(pm), aIConfigCateMapper.selectMapListPage(pm));
		return ds;
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<ConfigCate> loadByName(java.lang.String name){
		if(name!=null){
			return Optional.ofNullable(aIConfigCateMapper.selectByName(name));
		}
		return Optional.empty();
	}
	@Override
	public List<ConfigCate> loadAIConfigCates(Byte status) {
		
		return aIConfigCateMapper.selectAll( status);
	}
}